<?php

namespace app\wx\controller;

use think\Db;
use think\facade\Request;
use think\Validate;
use think\helper\Time;

class Stories extends Base
{
    protected $beforeActionList = [
        'first',
    ];
    protected $uid = '';

    protected function first()
    {
        if (session('uid') || input('openid')) {
            if (session('uid')) {
                $this->uid = session('uid');
            } else {
                $this->uid = DB::name('admin')->where('openid', input('openid'))->value('id');
                if($this->uid){
                    session('uid', $this->uid);
                }else{
                    $url=url('/wx/interest/index' , '', '', Request::host());
                    $this->redirect($url);
                }
            }
        } else {
            $url = url('stories/index?stories_id=' . input('stories_id'), '', '', Request::host());
            $this->redirect('https://openwx.weibuyun.com/wxuser/get_openid' . '?domain=' . Request::host() . '&url=' . $url);
        }
    }

    public function wx_msg($info, $stories_id, $model_status = 0)
    {
        $post = $this->request->param();
        $data['project_id'] = $post['project_id'];
        $data['model_id'] = $post['model_id'];
        $data['info'] = $info;
        $data['stories_id'] = $stories_id;
        $data['c_time'] = time();
        $data['uid'] = session('uid');
        if ($model_status == 0) {
            $data['model_status'] = Db::name('stories')->where('id', $stories_id)->cache(60)->value('status');
        } else {
            $data['model_status'] = $model_status;
        }
        DB::name('wx_msg')->insert($data);
    }

    public function index()
    {
        if ($this->request->isPost()) {

            Db::startTrans();
            try {
                $post = $this->request->param();
                $post['c_time'] = time();
                $post['uid'] = session('uid');
                Db::name('comment')->strict(false)->insert($post);
                $project_model = config('param.project_model');
                $this->wx_msg('添加新的' . $project_model[$post['model_id']]. '讨论(' . $post['info'] . ')', $post['stories_id']);
                Db::commit();
                $status = 1;
            } catch (\Exception $e) {
                Db::rollback();
                $status = 0;
            }
            if ($status) {
                $this->success('操作成功!');
            }
            $this->error('操作失败');

        } else {
            $info = Db::view('stories', 'id,project_id,title,touid,s_time,e_time,w_time,uid,p_time,level_id,status,delete_time,category,model_id,info_id')
                ->view(['admin' => 'adminuid'], ['nickname' => 'uidnickname'], 'stories.uid=adminuid.id', 'LEFT')
                ->view(['admin' => 'admintouid'], ['nickname' => 'touidnickname'], 'stories.touid=admintouid.id', 'LEFT')
                ->where('stories.id', input('stories_id'))
                //->where('stories.uid|stories.touid','=',session('uid'))
                //->fetchSql(true)
                ->find();
            if (DB::name('project_user')->where('project_id', $info['project_id'])->where('uid', session('uid'))->find()) {

            } else {
                $this->assign('info', '你不能查看当前内容');
                return view('work/error');
            }
            $project_model = config('param.project_model');
            $info['model_name'] = $project_model[$info['model_id']];
            //echo $data['data'];
            $info['project_title'] = DB::name('project')->where('id', $info['project_id'])->value('title');
            $stories_status = config('param.model_' . $info['model_id'] . '_status');
            $level_status = config('param.level_status');
            $info['status'] = $stories_status[$info['status']];
            $info['level_id'] = $level_status[$info['level_id']];
            $info['s_time'] == 0 ? $info['s_time'] = '--' : $info['s_time'] = date('m-d', $info['s_time']);
            $info['e_time'] == 0 ? $info['e_time'] = '--' : $info['e_time'] = date('m-d', $info['e_time']);
            $info['info'] = DB::name('infoview')->where('id', $info['info_id'])->value('info');
            $info['info'] = str_replace("/qiniu/downloadurl?file=", "/wx/qiniu/downloadurl?file=", $info['info']);
            $this->assign('info', $info);
            $comment = DB::view('comment', 'id,uid,c_time,info')
                ->view('admin', 'nickname', 'comment.uid=admin.id')
                ->where('stories_id', input('stories_id'))
                ->order('id desc')->select();

            foreach ($comment as &$item) {
                $item['c_time'] = date('Y-m-d  H:i:s', $item['c_time']);
                $item['info'] = str_replace("/qiniu/downloadurl?file=", "/wx/qiniu/downloadurl?file=", $item['info']);
            }
            $this->assign('comment', $comment);
            return view();
        }


    }

    public function model_lists()
    {
        $map = array();
        $data = array();
        $post = $this->request->param();
        $map[] = ['stories.delete_time', '=', 0];
        $map[] = ['stories.uid|stories.touid', '=', $this->uid];
        $map[] = ['stories.status', '<', 4];
        $map[] = ['stories.model_id', '=', $post['model_id']];
        $data['count'] = Db::view('stories', 'id,project_id,title,touid,s_time,e_time,w_time,uid,p_time,level_id,status,delete_time,category,model_id')
            ->view(['admin' => 'adminuid'], ['nickname' => 'uidnickname'], 'stories.uid=adminuid.id', 'LEFT')
            ->view(['admin' => 'admintouid'], ['nickname' => 'touidnickname'], 'stories.uid=admintouid.id', 'LEFT')
            ->view('project', ['title' => 'project_title'], 'stories.project_id=project.id')
            ->where($map)
            ->count();
        $data['data'] = Db::view('stories', 'id,project_id,title,touid,s_time,e_time,w_time,uid,p_time,level_id,status,delete_time,category,model_id')
            ->view(['admin' => 'adminuid'], ['nickname' => 'uidnickname'], 'stories.uid=adminuid.id', 'LEFT')
            ->view(['admin' => 'admintouid'], ['nickname' => 'touidnickname'], 'stories.touid=admintouid.id', 'LEFT')
            ->view('project', ['title' => 'project_title'], 'stories.project_id=project.id')
            ->where($map)
            //->fetchSql(true)
            ->order(['status', 'id' => 'desc',])->page(input("param.page"), input('param.limit'))->select();
        //echo $data['data'];

        $stories_status = config('param.model_' . $post['model_id'] . '_status');
        $level_status = config('param.level_status');
        foreach ($data['data'] as &$info) {
            $info['status'] = $stories_status[$info['status']];
            $info['level_id'] = $level_status[$info['level_id']];
            $info['title'] = $info['project_title'] . '#' . $info['title'];
            $info['s_time'] == 0 ? $info['s_time'] = '--' : $info['s_time'] = date('m-d', $info['s_time']);
            $info['e_time'] == 0 ? $info['e_time'] = '--' : $info['e_time'] = date('m-d', $info['e_time']);
        }
        if ($data['count'] > 0 && $data['data']) {
            $data['msg'] = '获取成功';
        } else {
            $data['msg'] = '获取数据为空';
        }
        $data['code'] = 0;
        return json($data);
    }


    //临期
    public function workindex()
    {
        return view();

    }

    public function work_lists()
    {
        $map = array();
        $data = array();
        $post = $this->request->get();

        unset($post['page']);
        unset($post['limit']);
        unset($post['status']);
        $post['status'] = 1;
        $map[] = ['uid', '=', $this->uid];
        $sm = Time::daysAgo(60);
        $data['data']['sm'] = date('Y-m-1', $sm);
        list($sm, $em) = Time::Month();
        $data['data']['em'] = date('Y-m-d', $em);
        $userinfo = Db::view('admin', 'id,nickname')
            ->view('auth_group_access', 'group_id', 'admin.id=auth_group_access.uid')
            ->field('id,nickname')
            ->where($map)
            ->find();
        $data['data']['nickname'] = $userinfo['nickname'];
        $list = DB::name('work_user_time')->where($map)->where('c_time', '>', $sm)->field('c_time,w_time')->select();
        foreach ($list as &$item) {
            $item['c_time'] = date('Y-m-d', $item['c_time']);
            $item['w_time'] = $item['w_time'];
        }
        $data['data']['data'] = $list;
        if ($data['data']) {
            $data['msg'] = '获取成功';
        } else {
            $data['msg'] = '获取数据为空';
        }
        $data['code'] = 0;
        return json($data);

    }


}
